﻿using Furion.DynamicApiController;
using Furion.FriendlyException;
using Gear.Application.ErrorControl;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.Web.Tests.Characteristic
{
    /// <summary>
    /// 异常抛出
    /// </summary>
    [DynamicApiController]
    [ApiDescriptionSettings("ExceptionHandling")]
    public class ThrowOut
    {
        /// <summary>
        /// 基础异常抛出
        /// </summary>
        /// <param name="message"></param>
        public void ErrorOut(string message)=> throw Oops.Oh(message);
        /// <summary>
        /// 抛出特定类型异常
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ErrorOutV1(int id)
        {
            if (id < 2) throw Oops.Oh(TestCodes.z1000, id, 2);
            if (id < 3) throw Oops.Oh($"{id} 不能小于3。", typeof(InvalidOperationException));
            
            return id;
        }
        /// <summary>
        /// 自定义规则异常抛出
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [IfException(ErrorMessage = "全局异常拦截")]
        [IfException(TestCodes.z1000, ErrorMessage = "我覆盖了默认的：{0} 不能小于 {1}")]
        [IfException(TestCodes.x1001, "格式化参数1", "格式化参数2", ErrorMessage = "我覆盖了默认的：{0} 不能小于 {1}")]
        [IfException(TestCodes.x1000, "格式化参数1", "格式化参数2")]
        [IfException(TestCodes.SERVER_ERROR, "格式化参数1", "格式化参数2")]
        public int ErrorOutV2(int id)
        {
            if (id < 3)
            {
                throw Oops.Oh(TestCodes.z1000, id, 3);
                //throw Oops.Oh(1000);
                //throw Oops.Oh(ErrorCodes.x1000);
                //throw Oops.Oh("哈哈哈哈");
                //throw Oops.Oh(errorCode: "x1001");
                //throw Oops.Oh(1000, typeof(Exception));
                //throw Oops.Oh(1000).StatusCode(400);
            }

            return id;
        }
        /// <summary>
        /// 带返回值异常重载
        /// </summary>
        /// <returns></returns>
        //public int ErrorOutV3()
        //{
        //  return Oops.Retry<int>(() =>
        //    {
        //        return 1;
        //    }, 3, 1000);
        //}
    }
}
